package com.probiz.estoresa.order.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.common.model.order.OrderShipment;
import com.probiz.estore.order.service.OrderShipmentManager;

public class OrderReturnController extends GenericStoreAdminController<OrderShipment> {
    private OrderShipmentManager orderShipmentManager = null;

    public void setOrderShipmentManager(OrderShipmentManager inMgr) {
        this.orderShipmentManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(OrderShipment entity) {
		return entity.getOrderShipmentName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = orderShipmentManager;
	}
	
	@Override
	protected Map<String,String> getSortMap4Direction(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "documentNo asc");
		sortMap.put("2", "documentNo desc");
		sortMap.put("3", "salesOrder.orderNo asc");
		sortMap.put("4", "salesOrder.orderNo desc");
		sortMap.put("5", "shippingCost asc");
		sortMap.put("6", "shippingCost desc");
		sortMap.put("7", "convert_gbk(deliveryTypeDesc) asc");
		sortMap.put("8", "convert_gbk(deliveryTypeDesc) desc");
		sortMap.put("9", "createTime asc");
		sortMap.put("10", "createTime desc");
		sortMap.put("11", "trackingNo asc");
		sortMap.put("12", "trackingNo desc");
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, OrderShipment entity, BindException errors) {
	}
	
	public ModelAndView search(HttpServletRequest request, HttpServletResponse response) {
		SearchCriteria sc = createSearchCriteria(request, OrderShipment.FilterHqlName.SEARCH_4_RETURN);
		List results = searchByCriteria(sc);
		return getModelAndView(listView, listModelName, results);
	}

	/**
	 * 打印退货单
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView printReturnList(HttpServletRequest request, HttpServletResponse response) {
		List<OrderShipment> orderShipmentList = new ArrayList<OrderShipment>();
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String id: ids) {
				OrderShipment op=orderShipmentManager.getById(Integer.valueOf(id));
				orderShipmentList.add(op);
			}
		}
		ModelAndView mv = new ModelAndView("order/print/orderReturnPrintList");
		mv.addObject("orderShipmentList", orderShipmentList);
		return mv;
	}
	
	/**
	 * 保存物流单号
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView saveTrackingNo(HttpServletRequest request, HttpServletResponse response) {
		String[] trackingNos = request.getParameterValues("trackingNo");
		String[] salesOrderIds = request.getParameterValues("orderShipmentId");
		if (trackingNos != null && trackingNos.length > 0) {
			for (int i=0; i<trackingNos.length; i++) {
				Integer salesOrderId = Integer.valueOf(salesOrderIds[i]);
				OrderShipment orderShipment =orderShipmentManager.getById(salesOrderId);
				orderShipment.setTrackingNo(trackingNos[i]);
				orderShipmentManager.save(orderShipment);
			}
		}
		saveMessage(request, getMessage("orderShippment.saveTrackingNo.success"));
		return search(request,response);
	}

}
